from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


class Base():

    def __init__(self, driver):
        self.driver = driver
        # self.driver = webdriver.Edge()

    def open_url(self, url):
        self.driver.get(url)

    def time(self, name):
        t = time.strftime('%Y%m%d%H%S%M', time.localtime())
        self.driver.save_screenshot(t+name)

    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: driver.find_element(*loc).isdisplay())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(f'{e}')
            self.time('error.png')

    def click(self, loc):
        self.find_element(loc).click()

    def send_keys(self, loc, info):
        self.find_element(loc).send_keys(info)

    def accept_alert(self):
        alert = self.driver.switch_to.alert
        alert.accept()
        return alert.text

    def dismiss_alert(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()

    def accept_frame(self, loc):
        f = self.find_element(loc)
        self.driver.switch_to.frame(f)

    def dismiss_frame(self):
        self.driver.switch_to.default_content()

    def gettext(self, loc):
        return self.find_element(loc).text












